package comm.util;

import com.fasterxml.jackson.core.type.TypeReference;
import comm.util.JsonUtils;
import model.PageResult;
import model.Result;
import model.activity.HdOpenActivityVO;

public class HdOpenHttpUtils extends HdOpenUtilBase {


    /**
     * 获取活动列表
     *
     * @Param aid 必须 凡科用户标识
     * @Param activityType 非必须 活动类型（默认全部查询全部-1）
     * @Param sort 非必须 排序（默认id）
     * @Param pageNo 非必须 页数（默认1）
     * @Param pageSize 非必须 返回的最大记录数（默认10）
     * @Author: junes 2022/9/14 19:54
     * @Update: junes 2022/9/14 19:54
     */
    public static PageResult<HdOpenActivityVO> getActivitiesList(String accessToken, int aid, int activityType, String sort, int pageNo, int pageSize) throws Exception {
        String url = API_URL + "/open-api/activities/list-page";
        OkHttpUtils httpUtils = OkHttpUtils.builder()
                .url(url)
                .addHeader("Authorization", "Bearer " + accessToken)
                .addParam("aid", String.valueOf(aid))
                .addParam("activityType", String.valueOf(activityType))
                .addParam("sort", sort)
                .addParam("pageNo", String.valueOf(pageNo))
                .addParam("pageSize", String.valueOf(pageSize));

        String resultJson = httpUtils.get().sync();
        TypeReference typeReference = new TypeReference<PageResult<HdOpenActivityVO>>() {
        };

        return (PageResult<HdOpenActivityVO>) JsonUtils.nativeRead(resultJson, typeReference);
    }

    /**
     * 获取活动详情
     *
     * @Param aid 必须 凡科用户标识
     * @Param activityId 必须 活动id
     * @Author: junes 2022/9/14 19:54
     * @Update: junes 2022/9/14 19:54
     */
    public static Result<HdOpenActivityVO> getActivitiesInfo(String accessToken, int aid, int activityId) {
        String url = API_URL + "/open-api/activities/one";
        OkHttpUtils httpUtils = OkHttpUtils.builder()
                .url(url)
                .addHeader("Authorization", "Bearer " + accessToken)
                .addParam("aid", String.valueOf(aid))
                .addParam("activityId", String.valueOf(activityId));

        String resultJson = httpUtils.get().sync();
        TypeReference typeReference = new TypeReference<Result<HdOpenActivityVO>>() {
        };

        return (Result<HdOpenActivityVO>) JsonUtils.nativeRead(resultJson, typeReference);
    }

    /**
     * 拼接活动地址
     *
     * @Author: junes 2022/9/16 17:44
     * @Update: junes 2022/9/16 17:44
     */
    public static String jointActivityUrl(String accessToken, String url) {
        return url + "&partner_access_token=" + accessToken;
    }

}
